package model;

import com.sun.org.apache.regexp.internal.RE;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Administrator
 * Date: 2023-01-13
 * Time: 下午 21:36
 */

//博客页面要实现的功能
//1.新增博客(博客编辑页)
//2.查询博客列表(博客列表页)
//3.查询出指定博客详情(博客详情页)
//4.删除指定的博客(博客详情业页)
public class BlogDao {

    //新增博客
    public void insert(Blog blog) {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            //建立链接
            connection = DBUtil.getConnection();
            //构建sql代码
            String sql = "insert into blog values(null,?,?,?,now())";
            statement = connection.prepareStatement(sql);
            statement.setString(1,blog.getTitle());
            statement.setString(2,blog.getContent());
            statement.setInt(3,blog.getUserID());
            //执行sql语句
            int ret = statement.executeUpdate();

            if(ret == 1) {
                System.out.println("插入成功!");
            }else {
                System.out.println("插入失败!");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,null);
        }
    }

    //由于这个函数是个博客列表页使用的,也就是说它不用把博客正文全部显示出来
    //只需要显示一部分就可以了,所以用substring来截取一定的字符,防止列表页显示不全
    public List<Blog> selectAll() {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        List<Blog> list = new ArrayList<>();
        try {
            //建立连接
            connection = DBUtil.getConnection();
            //构建sql字符串
            String sql = "select * from blog order by postTime desc";
            statement = connection.prepareStatement(sql);
            //执行sql语言
            resultSet = statement.executeQuery();
            //便利结果集合
            while(resultSet.next()) {
                Blog blog = new Blog();
                blog.setBlogID(resultSet.getInt("blogID"));
                blog.setTitle(resultSet.getString("title"));
                String content = resultSet.getString("content");
                if(content.length() > 100) {
                    content = content.substring(0,100);
                }
                blog.setContent(content);
                blog.setUserID(resultSet.getInt("userID"));
                blog.setPostTime(resultSet.getTimestamp("postTime"));
                list.add(blog);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,statement,resultSet);
        }

        return list;
    }

    public Blog selectOne(int blogID) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;

        try {
            //建立连接
            connection = DBUtil.getConnection();
            //构建sql字符串
            String sql = "select * from blog where blogID = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,blogID);
            //执行sql语句
            resultSet = statement.executeQuery();

            //这里需要注意由于这里查询的列是自增主键,自增主键是不能重复的,
            //所以是不可能有多条数据的,要么是查到了为1条数据,要么没查到0条数据.
            if(resultSet.next()) {
                Blog blog = new Blog();
                blog.setBlogID(resultSet.getInt("blogID"));
                blog.setTitle(resultSet.getString("title"));
                blog.setContent(resultSet.getString("content"));
                blog.setUserID(resultSet.getInt("userID"));
                blog.setPostTime(resultSet.getTimestamp("postTime"));

                return blog;
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return  null;
    }

    public void delete(int blogID) {
        Connection connection = null;
        PreparedStatement statement = null;


        try {
            //建立连接
            connection = DBUtil.getConnection();
            //构建sql语句
            String sql = "delete from blog where blogID = ?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1,blogID);
            //执行sql语句
            int ret = statement.executeUpdate();
            if(ret == 1) {
                System.out.println("删除成功!");
            }else {
                System.out.println("删除失败!");
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,statement,null);
        }
    }
}
